package leetcode;

/**
 * @author Liangyaoyun
 */
public class leetCode121 {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        int res = 0;
        int buyindex = 0;
        for (int i = 1; i < len; i++) {
            int profit = prices[i] - prices[i - 1];
            if (profit > 0 && res <= profit || prices[buyindex] > prices[i - 1] && profit > 0) {
                if (res <= profit)
                    res = profit;
                buyindex = i - 1;
                for (int j = i + 1; j < len; j++) {
                    profit = prices[j] - prices[buyindex];
                    if (res < profit) {
                        res = profit;
                    }
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{2, 4, 1, 7, 11};
        leetCode121 leetCode121 = new leetCode121();
        System.out.println(leetCode121.maxProfit(nums));
    }
}
